/*
 * Copyright (c) 2020 - present, Inspur Genersoft Co., Ltd.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/*
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.inspur.edp.cef.generator.apiGenerator.dataType.entityInterface.resourceInfo;

import com.inspur.edp.caf.generator.baseInfo.ParameterInfo;
import com.inspur.edp.caf.generator.baseInfo.TypeInfo;
import com.inspur.edp.caf.generator.method.ClassMethodGenerator;
import com.inspur.edp.caf.generator.utils.ExpressionUtils;
import com.inspur.edp.cef.designtime.api.IGspCommonField;
import com.inspur.edp.cef.generator.apiGenerator.dataType.entityInterface.DataTypeInterfaceGeneratorContext;
import com.inspur.edp.cef.spi.entity.info.UniqueConstraintInfo;
import com.inspur.edp.das.commonmodel.IGspCommonObject;
import com.inspur.edp.das.commonmodel.entity.object.GspUniqueConstraint;
import org.eclipse.jdt.core.dom.*;

import java.util.ArrayList;

public class InitUQConsGenerator extends ClassMethodGenerator {

    private String methodName = "initUQConstraintInfos";
    private IGspCommonObject dataType;
    private DataTypeInterfaceGeneratorContext parentContext;
    private int countField =0;
    protected InitUQConsGenerator(IGspCommonObject dataType, DataTypeInterfaceGeneratorContext parentContext) {
        this.dataType = dataType;
        this.parentContext = parentContext;
    }

    @Override
    protected String getMethodName() {
        return methodName;
    }

    @Override
    protected TypeInfo getReturnType() {
        return null;
    }

    @Override
    protected ArrayList<Modifier.ModifierKeyword> getAccessModifiers() {
        ArrayList<Modifier.ModifierKeyword> list = new ArrayList<>();
        list.add(Modifier.ModifierKeyword.PROTECTED_KEYWORD);
        return list;
    }

    @Override
    protected ArrayList<ParameterInfo> getParameterCollection() {
        return new ArrayList<>();
    }

    @Override
    protected boolean getIsOverride() {
        return true;
    }

    @Override
    protected Block buildMethodBody() {
        addImport(ArrayList.class.getTypeName());
        Block block = ast.newBlock();

        for (GspUniqueConstraint con : dataType.getContainConstraints()) {
            buildConstraintInfo(block, con);
        }

        return block;
    }

    private void buildConstraintInfo(Block block, GspUniqueConstraint con) {
        MethodInvocation invocation = ast.newMethodInvocation();
        invocation.setName(ast.newSimpleName("addUQConstraintInfo"));
        StringLiteral stringLiteralCode = ast.newStringLiteral();
        stringLiteralCode.setLiteralValue(con.getCode());
        invocation.arguments().add(stringLiteralCode);

        if ((con.getI18nResourceInfoPrefix()==null||con.getI18nResourceInfoPrefix().equals(""))||(!con.getI18nResourceInfoPrefix()
            .substring(0, con.getI18nResourceInfoPrefix().lastIndexOf("."))
            .equals(dataType.getI18nResourceInfoPrefix())) )
        {
            StringLiteral stringLiteral=ast.newStringLiteral();
            stringLiteral.setLiteralValue( con.getI18nResourceInfoPrefix() + "." + "TipInfo");
            invocation.arguments().add(stringLiteral);
        }

//        ClassInstanceCreation classInstanceCreation = ast.newClassInstanceCreation();
//        classInstanceCreation.setType(new TypeInfo(ArrayList.class).getType(ast));
//        AnonymousClassDeclaration anonymousClassDeclaration = ast.newAnonymousClassDeclaration();

//        Block block1=ast.newBlock();
//        for(String elementId: con.getElementList()){
//            for(IGspCommonField commonField:this.dataType.getContainElements()){
//                if(commonField.getID().equals(elementId)){
//                    MethodInvocation methodInvocation = ast.newMethodInvocation();
//                    methodInvocation.setName(ast.newSimpleName("add"));
//                    StringLiteral stringLiteral = ast.newStringLiteral();
//                    stringLiteral.setLiteralValue(commonField.getLabelID());
//                    methodInvocation.arguments().add(stringLiteral);
//                    ExpressionStatement expressionStatement = ast.newExpressionStatement(methodInvocation);
//                    block1.statements().add(expressionStatement);
//                    break;
//                }
//            }
//        }
        StringBuilder stringBuilder=new StringBuilder();
        for(String elementId: con.getElementList()){
            for(IGspCommonField commonField:this.dataType.getContainElements()){
                if(commonField.getID().equals(elementId)){
                    stringBuilder.append(commonField.getLabelID());
                    stringBuilder.append(",");
                }
            }
        }
        String str ="";
        if(stringBuilder.length()!=0)
            str=   stringBuilder.substring(0,stringBuilder.length()-1);
        StringLiteral stringLiteral=ast.newStringLiteral();
        stringLiteral.setLiteralValue(str);
        invocation.arguments().add(stringLiteral);
        block.statements().add(ast.newExpressionStatement(invocation));
    }

    private void addUQCode(Block block, GspUniqueConstraint con, String constraintVarName){
        String listName = "list" + countField++;
        ParameterizedType parameterizedType = ast.newParameterizedType(ast.newSimpleType(ast.newSimpleName("ArrayList")));
        parameterizedType.typeArguments().add(ast.newSimpleType(ast.newSimpleName("String")));
        VariableDeclarationFragment fragment = ast.newVariableDeclarationFragment();
        fragment.setName(ast.newSimpleName(listName));
        ClassInstanceCreation classInstanceCreation = ast.newClassInstanceCreation();
        ParameterizedType p = ast.newParameterizedType(ast.newSimpleType(ast.newSimpleName("ArrayList")));
        classInstanceCreation.setType(p);
        fragment.setInitializer(classInstanceCreation);

        VariableDeclarationStatement variableDeclarationStatement =  ast.newVariableDeclarationStatement(fragment);
        variableDeclarationStatement.setType(parameterizedType);
        block.statements().add(variableDeclarationStatement);
        for(String elementId: con.getElementList()){
            for(IGspCommonField commonField:this.dataType.getContainElements()){
                if(commonField.getID().equals(elementId)){
                    MethodInvocation methodInvocation = ast.newMethodInvocation();
                    methodInvocation.setExpression(ast.newSimpleName(listName));
                    methodInvocation.setName(ast.newSimpleName("add"));
                    StringLiteral stringLiteral = ast.newStringLiteral();
                    stringLiteral.setLiteralValue(commonField.getLabelID());
                    methodInvocation.arguments().add(stringLiteral);
                    ExpressionStatement expressionStatement = ast.newExpressionStatement(methodInvocation);
                    block.statements().add(expressionStatement);
                    break;
                }
            }
        }


        MethodInvocation setFieldsMethod = ast.newMethodInvocation();
        setFieldsMethod.setExpression(ast.newSimpleName(constraintVarName));
        setFieldsMethod.setName(ast.newSimpleName("setFields"));
        setFieldsMethod.arguments().add(ast.newSimpleName(listName));
        ExpressionStatement expressionStatement = ast.newExpressionStatement(setFieldsMethod);
        block.statements().add(expressionStatement);

        MethodInvocation setNodeMethod = ast.newMethodInvocation();
        setNodeMethod.setExpression(ast.newSimpleName(constraintVarName));
        setNodeMethod.setName(ast.newSimpleName("setNodeCode"));
        StringLiteral stringLiteral = ast.newStringLiteral();
        stringLiteral.setLiteralValue(this.dataType.getCode());
        setNodeMethod.arguments().add(stringLiteral);
        ExpressionStatement setNodeCode = ast.newExpressionStatement(setNodeMethod);
        block.statements().add(setNodeCode);
    }
    private Statement getCreateInfoStatement(String varName) {
        TypeInfo info = new TypeInfo(UniqueConstraintInfo.class);
        VariableDeclarationFragment frag = this.ast.newVariableDeclarationFragment();
        frag.setName(this.ast.newSimpleName(varName));
        ClassInstanceCreation creation = this.ast.newClassInstanceCreation();
        creation.setType(info.getType(this.ast,parentContext.getApiGenContext()));
        frag.setInitializer(creation);
        VariableDeclarationStatement statement = this.ast.newVariableDeclarationStatement(frag);
        statement.setType(info.getType(this.ast));
        return statement;
    }

    private Statement getSetDisplayNameStatement(String varName, String key) {
        MethodInvocation invocation = ExpressionUtils.getMethodInvocation(this.ast, varName, "setDisplayValueKey");
        invocation.arguments().add(ExpressionUtils.getStringLiteral(this.ast, key));
        return this.ast.newExpressionStatement(invocation);
    }
}
